package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;


    /**
     * 添加检查项
     *
     * @param checkItem 检查项
     * @return 更新的行数是否不为0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CheckItem checkItem) {
        try {
            List<CheckItem> items = checkItemMapper.findPageByQueryString(checkItem.getCode());
            if (items != null && !items.isEmpty()) {
                log.info("添加失败，检查项已存在{}", items);
                return false;
            } else {
                log.info("添加检查项{}", checkItem);
                return checkItemMapper.insert(checkItem) > 0;
            }

        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 删除检查项
     *
     * @param id id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        // 检查项与检查组无关联才能删除
        if (checkItemMapper.findGroupIdsByCheckItemId(id).isEmpty()) {
            checkItemMapper.deleteById(id);
            log.info("删除检查组成功{}", id);
            return true;
        } else {
            log.info("检查项与检查组有关联，不能删除");
            return false;
        }
    }

    /**
     * 修改检查项
     *
     * @param checkItem 检查项
     */
    @Override
    public Boolean edit(CheckItem checkItem) {
        return checkItemMapper.edit(checkItem) > 0;
    }


    /**
     * 分页查询
     *
     * @param queryPageBean 查询条件实体
     * @return 查询结果实体
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        // 1.分页数据
        Integer currentPage = queryPageBean.getCurrentPage(), pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        // 2.空值判断
        // 若是查询分页为空，则设置默认值
        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 10;
        if (queryString == null) queryString = "";

        // 分页查询
        PageHelper.startPage(currentPage, pageSize);
        // 查询结果
        Page<CheckItem> pages = (Page<CheckItem>) checkItemMapper.findPageByQueryString(queryString);
        // 封装结果
        return new PageResult(pages.getTotal(), pages.getResult());
    }

    /**
     * 查询所有检查项
     *
     * @return 查询检查项结果列表
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }

    /**
     * 根据id查询检查项
     *
     * @param id id
     * @return 检查项
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    /**
     * 根据检查组id查询检查项id
     *
     * @param id id
     * @return 检查项id列表
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupCheckItemMapper.findItemIdsByCheckGroupId(id);

    }

}
